/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Modulo3;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.json.*;
import java.io.PrintWriter;
import Modelo.DAL.ConexionBD;
import java.sql.*;

/**
 *
 * @author Hector Gutierrez
 */
public class listaTiposRiesgo extends org.apache.struts.action.Action {
    
    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    
    @Override
  public ActionForward execute(ActionMapping mapping, ActionForm form,
          HttpServletRequest request, HttpServletResponse response)
          throws Exception {

    response.setContentType("text/json");
    JSONObject data = new JSONObject();

    data.put("TiposRiesgo", consultarTiposRiesgo());
    PrintWriter pw = response.getWriter();
    pw.write(data.toString());
    pw.flush();
    pw.close();

    return mapping.findForward(SUCCESS);
  }

  public JSONObject consultarTiposRiesgo() {
    ConexionBD conex = new ConexionBD();
    JSONObject tiposRiesgo = new JSONObject();

    try {
      Statement st = conex.session.createStatement();

      ResultSet rs = st.executeQuery("SELECT nombre FROM tipo_riesgo ORDER BY nombre DESC");
      int i = 0;
      while (rs.next()) {
        JSONObject tipoRiesgo = new JSONObject();
        tipoRiesgo.put("nombre", rs.getString(1));
        tiposRiesgo.put("" + i++, tipoRiesgo);
      }
    } catch (Exception e) {
    }
    return tiposRiesgo;
  }
}
